package com.grad.webstudy.service.impl;

import com.grad.webstudy.constants.RedisConstants;
import com.grad.webstudy.dao.CourseDetailDao;
import com.grad.webstudy.enums.ExceptionEnums;
import com.grad.webstudy.exceptions.CustomException;
import com.grad.webstudy.model.CourseDetail;
import com.grad.webstudy.service.CourseChapterService;
import com.grad.webstudy.service.CourseDetailService;
import com.grad.webstudy.utils.RedisUtil;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/9 10:50.
 */

@Service
@Transactional
public class CourseDetailServiceImpl implements CourseDetailService {


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CourseDetailDao courseDetailDao;

    @Autowired
    private CourseChapterService courseChapterService;

    @Override
    public int add(CourseDetail courseDetail) {
        courseDetailDao.insert(courseDetail);
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX);
        return courseDetail.getId();
    }

    @Override
    public boolean addDetails(List<CourseDetail> courseDetails) {
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX);

        return courseDetailDao.insertDetails(courseDetails) > 0;
    }

    @Override
    public boolean deletes(String ids) {

        List<String> deleteIds = Arrays.asList(ids.split(","));
        if (deleteIds.size() <= 0) {
            throw new CustomException(ExceptionEnums.CUSTOM_ERROR.getCode(), "参数ids[" + ids + "]传入错误");
        }
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX);

        return courseDetailDao.deleteDetailByIds(deleteIds) == deleteIds.size();
    }

    @Override
    public boolean deleteByChapter(Integer chapterId) {
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX);

        return courseDetailDao.deleteDetailByChapterId(chapterId) > 0;
    }

    @Override
    public int deleteByChapterIds(String chapterIds) {

        List<String> ids = Arrays.asList(chapterIds.split(","));
        if (ids.size() <= 0) {
            throw new CustomException("参数chapterIds[" + chapterIds + "]错误");
        }
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX);

        return courseDetailDao.deleteDetailByIds(ids);
    }

    @Override
    public boolean update(CourseDetail courseDetail) {
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX);

        return courseDetailDao.updateDetail(courseDetail) > 0;
    }

    @Override
    public List<CourseDetail> queryByChapterId(Integer chapterId) {
        List<CourseDetail> result;
        String key = RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX + "chapterId-" + chapterId;
        if (redisUtil.hasKey(key)) {
            result = (List<CourseDetail>) redisUtil.get(key);
        } else {
            result = courseDetailDao.queryAllByChapter(chapterId);
            redisUtil.set(key, result);
        }

        return result;
    }

    @Override
    public CourseDetail queryById(Integer courseDetailId) {

        CourseDetail result;
        String key = RedisConstants.REDIS_COURSEDETAIL_KEY_PREFIX + "courseDetailId-" + courseDetailId;
        if (redisUtil.hasKey(key)) {
            result = (CourseDetail) redisUtil.get(key);
        } else {
            result = courseDetailDao.queryCourseDetailById(courseDetailId);
            redisUtil.set(key, result);
        }

        return result;

    }

    @Override
    public Integer queryCourseIdByDetailId(Integer courseDetailId) {
        CourseDetail courseDetail = courseDetailDao.queryCourseDetailById(courseDetailId);
        Integer chapterId = courseDetail.getChapterId();
        return courseChapterService.queryChapterById(chapterId).getCourseId();
    }
}
